<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\facade\Builder;
use think\response\Json;

class Recommend extends Base
{
    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\Recommend';

    //关联模型
    protected array $hasOne = ['level', 'qLevel','awardLevel'];

    public function index(): string
    {
        //构建表格数据
        return Builder::Table()
            ->addColumn(['type' => 'checkbox', 'title' => '全选', 'fixed' => 'left'])
            ->addColumn(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addColumn(['field' => 'referee', 'title' => '推荐人等级', 'templet' => '<div>{{d.level.title}}</div>'])
            ->addColumn(['field' => 'q_referee', 'title' => '被推荐人等级', 'templet' => '<div>{{d.qLevel.title}}</div>'])
            ->addColumn(['field' => 'generation', 'title' => '一代奖励', 'modify' => '2,ratio','templet' => '<span>{{(d.generation*100).toFixed(2)}}%</span>'])
            ->addColumn(['field' => 'secondary', 'title' => '二代奖励', 'modify' => '2,ratio','templet' => '<span>{{(d.secondary*100).toFixed(2)}}%</span>'])
            ->addColumn(['field' => 'other', 'title' => '三到十代奖励', 'modify' => '2,ratio','templet' => '<span>{{(d.other*100).toFixed(2)}}%</span>'])
            ->addColumn(['field' => 'other_amount', 'title' => '三到十代奖励金额', 'modify' => '2,input'])
            ->addColumn(['field' => 'one_everyday', 'title' => '一代每日', 'modify' => '2,input'])
            ->addColumn(['field' => 'one_capping', 'title' => '一代封顶', 'modify' => '2,input'])
            ->addColumn(['field' => 'two_monthly', 'title' => '二代每月', 'modify' => '2,input'])
            ->addColumn(['field' => 'two_capping', 'title' => '二代封顶', 'modify' => '2,input'])
            ->addColumn(['field' => 'create_time', 'title' => '创建时间'])
            ->addColumn(['field' => 'update_time', 'title' => '更新时间'])
            ->addColumn(['fixed' => 'right', 'title' => '操作', 'align' => 'center', 'toolbar' => '#barDemo', 'width' => 200])
            ->addRightButtons(['edit' => ['size' => '1000,1000']])
            ->addTopButtons(['add' => ['size' => '1000,1000'], 'deleteAll'])
            ->addSearch([])
            ->addSwitchTool([])
            ->addSortTool([])
            ->fetch();
    }

    public function save($param = [], $model = false): Json
    {
        $param = $this->request->post();
        $param['generation'] = $param['generation'] / 100;
        $param['secondary'] = $param['secondary'] / 100;
        $param['other'] = $param['other'] / 100;
        return parent::save($param, $model); // TODO: Change the autogenerated stub
    }

}
